package cn.tedu.cinema.cinemas.service.Impl;

import cn.tedu.cinema.cinemas.mapper.CinemasMapper;
import cn.tedu.cinema.cinemas.pojo.dto.CinemasQuery;
import cn.tedu.cinema.cinemas.pojo.dto.CinemasQueryUser;
import cn.tedu.cinema.cinemas.pojo.dto.CinemasSaveParam;
import cn.tedu.cinema.cinemas.pojo.entity.Cinemas;
import cn.tedu.cinema.cinemas.pojo.vo.CinemasVO;
import cn.tedu.cinema.cinemas.service.CinemasService;
import cn.tedu.cinema.hall.mapper.HallMapper;
import cn.tedu.cinema.hall.pojo.entity.Hall;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CinemasServiceImpl implements CinemasService {

    @Autowired
    CinemasMapper cinemasMapper;
    @Autowired
    private HallMapper hallMapper;
    @Override
    public void saveCinemas(CinemasSaveParam cinemasSaveParam) {
        log.debug("开始处理【添加/修改影院的业务】,参数{}", cinemasSaveParam);
        
        // 1. 准备影院信息
        Cinemas cinemas = new Cinemas();
        BeanUtils.copyProperties(cinemasSaveParam, cinemas);
        
        // 判断是新增还是修改
        if (cinemas.getId() == null) {
            // 新增操作
            cinemas.setCreateTime(new Date());
            cinemasMapper.insert(cinemas);
        } else {
            // 修改操作
            cinemasMapper.update(cinemas);
        }
        
        Long cinemaId = cinemas.getId();
        log.debug("影院ID: {}", cinemaId);
        
        // 2. 处理影厅数据
        if(cinemasSaveParam.getHalls() != null && cinemaId != null) {
            // 如果是修改操作，先删除原有的影厅数据
            if (cinemas.getId() != null) {
                hallMapper.deleteByCinemaId(cinemaId);
            }
            
            log.debug("开始保存影厅数据，数量: {}", cinemasSaveParam.getHalls().size());
            
            for(Hall hall : cinemasSaveParam.getHalls()) {
                log.debug("正在处理影厅: {}", hall);
                hall.setCinemaId(cinemaId);
                hall.setCreateTime(new Date());
                hall.setStatus("1");
                hall.setSeats(1000);
                
                try {
                    hallMapper.insert(hall);
                    log.debug("影厅保存成功: {}", hall.getName());
                } catch (Exception e) {
                    log.error("保存影厅失败: {}", e.getMessage(), e);
                    throw e;
                }
            }
        } else {
            log.warn("没有影厅数据需要保存");
        }
    }

    @Override
    public void deleteCinemas(Long id) {
        log.debug("开始处理【删除影院的业务】,参数{}", id);
        hallMapper.deleteByCinemaId(id);
        cinemasMapper.delete(id);
    }

    @Override
    public List<CinemasVO> selectCinemas(CinemasQuery cinemasQuery) {
        log.debug("开始处理【电影院查询】,参数{}", cinemasQuery);
        List<CinemasVO> list = cinemasMapper.select(cinemasQuery);
        return list;
    }
    

    @Override
    public List<CinemasVO> selectCinemaByHallTypeAndCityAndPlanMovieId(CinemasQueryUser cinemasQueryUser) {
        log.debug("开始处理【根据影厅类型、城市、电影id查询影院】,参数{}", cinemasQueryUser);
        List<CinemasVO> list = cinemasMapper.selectCinemaByHallTypeAndCityAndPlanMovieId(cinemasQueryUser);
        log.debug("查询结果: {}", list);
        return list;
    }

   
}
